package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/resp"
	"github.com/gin-gonic/gin"
)

func CreateOrder(c *gin.Context) {
	var req request.CreateOrderReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "验证失败", err.Error())
		return
	}
	order, err := config.SeckillClient.CreateActivityOrder(c, &__.CreateActivityOrderReq{
		UserId:     int64(req.UserId),
		ActivityId: int64(req.ActivityId),
		OrderNum:   int64(req.OrderNum),
	})
	if err != nil {
		resp.Error(c, 400, "创建订单失败", err.Error())
		return
	}

	resp.Success(c, "创建订单成功", order.PayUrl)
}

func PayOrder(c *gin.Context) {
	var req request.PayOrderReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "验证失败", err.Error())
		return
	}
	status, err := config.SeckillClient.UpdateOrderStatus(c, &__.UpdateOrderStatusReq{
		UserId:    c.GetInt64("userId"),
		OrderSn:   req.OrderSn,
		PayStatus: int32(req.PayStatus),
	})
	if err != nil {
		resp.Error(c, 400, "修改订单状态失败", err.Error())
		return
	}
	resp.Success(c, "修改订单状态成功", status.PayStatus)
}
